var cloudwalkobj;
var width=1;
var height=1;
var g_isIE6_8 = 0;
var CW_StrSeverIp = "120.25.161.56:7000";
var CW_appID = "user";
var CW_appSecret = "12345";
var ImgFrontData = "";
var ImgBackData = "";
var bIsClickOcrFront = false;
var bIsClickOcrBack = false;
var strLience = "";

function setIE6_8(flag) {
	g_isIE6_8 = flag;
}

function createPlugin(targetE) {
	cloudwalkobj = document.createElement("object");
	cloudwalkobj.width = width;
	cloudwalkobj.height = height;
	cloudwalkobj.id = "CloudWalkSDKPlugin";
	
	var bDet=BrowserDetect.browser;
	if("Explorer"==bDet)
	{
		cloudwalkobj.classid = "CLSID:B1597418-A51E-4140-8698-EE865439755C";	//IE
	}else if("Firefox"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Firefox
	}else if("Chrome"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Chrome
	}else{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";
	}

	targetE.appendChild(cloudwalkobj);
	
	if(!cloudwalkobj.valid){
		alert("插件未安装");
		return false;
	}
	
	registerCallBack(cloudwalkobj, "cwIdCardOCRCallBack", idcardOcrEvent);
	
	var config = cloudwalkobj.cwGetConfig();
	PraseConfig(config);

	var nret = cloudwalkobj.cwIDCardInit(strLience);
	if(nret != 0)
	{
		var msg = "初始化SDK失败错误码：" + nret;
		alert(msg);
	}
	
	return true;
}

function idcardOcrEvent(jsonStr) {
	//var jsonObj = JSON.parse(jsonStr);//IE6、IE8不兼容
	
	//alert(jsonStr); //jinfude add
	
	var jsonObj = $.parseJSON(jsonStr);
	var str="";
	if(jsonObj.result != 0){
		
		str = "";
		if(jsonObj.result == CW_couldntConnect){
			str += "网络异常！";
		}else if(jsonObj.result == EnumLocalNetErrorSerAddr){
			str += "无法解析人脸识别服务主机地址！";
		}else if(jsonObj.result == EnumLocalNetErrorConSer){
			str += "无法连接到人脸识别服务主机！";
		}else if(jsonObj.result == CW_TimeOut){
			str += "访问人脸识别服务主机超时！";
		}else if(jsonObj.result == CW_NORIGHT){
			str += "没有权限";
		}
		else{
			str += "OCR失败！失败信息：";
			str += jsonObj.resultInfo;
		}

	}
	else{
		var check = jsonObj.type;
		if(check == 1){
			str = "";
			str += "姓名:";
			str += jsonObj.name;
			str += "<br/>性别:";
			str += jsonObj.sex;
			str += "<br/>民族:";
			str += jsonObj.people;
			str += "<br/>生日:";
			str += jsonObj.birthday;
			str += "<br/>地址:";
			str += jsonObj.address;
			str += "<br/>身份证号:";
			str += jsonObj.id_number;
			str += "";
			str += "<br>";
			
			//第三种解决方法，将Base64图片发布到服务器保存，由服务器提供对外访问的图片url，再设置img.src = url;即可，这里省略
		}else{
			str = "";
			str += "签发机关:";
			str += jsonObj.issue_authority;
			str += "<br/>有效期限:";
			str += jsonObj.validity;
			str += "";
			str += "<br>";
			
		}
	}
	
	if(bIsClickOcrFront)
	{
		document.getElementById("result").innerHTML = str;
		bIsClickOcrFront = false;
	}
	else if(bIsClickOcrBack)
	{
		document.getElementById("result2").innerHTML = str;
		bIsClickOcrBack = false;
	}
	
	hideLoading();
	//EnableButton();
	
}

function click_browseIDCardFilePathFront() {
	var imgFrontPath=cloudwalkobj.cwSelectFile();
	if(imgFrontPath.length == 0)
	{
		return ;
	}
	
	document.getElementById("fileText").value = "";
	document.getElementById("imghead").src = "../images/zhengmian.png";
	document.getElementById("result").innerHTML = "";
	$("#question1").css("display","block");
	ImgFrontData = "";
	
	var ImgFrontTemp = cloudwalkobj.cwGetBase64ByLocalFile(imgFrontPath);
	if(ImgFrontTemp == null || ImgFrontTemp == "")
	{
		alert("加载图片失败");
		return;
	}

	
	var jsonStr = cloudwalkobj.cwIDCardCheck(ImgFrontTemp);
	var jsonObj = $.parseJSON(jsonStr);
	if(jsonObj != null)
	{
		if(jsonObj.result == 0)
		{
			var nflag = jsonObj.flag;
			if(nflag == 0)
			{
				alert("请选择正确的身份证图片！");
				return;
			}
	
		}
		else
		{
			var msg = "请选择正确的身份证图片！";
			alert(msg);
			return;
		}
	}
	else{
		var msg = "检测模块初始化未成功！";
		alert(msg);
		return;
	}
	
	ImgFrontData = jsonObj.IdCardImg;
	document.getElementById("fileText").value = imgFrontPath;
	var imgObj = document.getElementById("imghead");
	showImg(imgObj, imgFrontPath, ImgFrontData);
}

function click_browseIDCardFilePathBack(){
	var ImgBackPath = cloudwalkobj.cwSelectFile();
	if(ImgBackPath.length == 0)
	{
		return ;
	}
	
	document.getElementById("fileText2").value = "";
	document.getElementById("imghead2").src = "../images/beimian.png";
	document.getElementById("result2").innerHTML = "";
	$("#question2").css("display","block");
	ImgBackData = "";
	
	var ImgBackTemp =  cloudwalkobj.cwGetBase64ByLocalFile(ImgBackPath);
	if(ImgBackTemp == null || ImgBackTemp == "")
	{
		alert("加载图片失败");
		return;
	}
	
	var jsonStr = cloudwalkobj.cwIDCardCheck(ImgBackTemp);
	var jsonObj = $.parseJSON(jsonStr);
	if(jsonObj != null)
	{
		if(jsonObj.result == 0)
		{
			
			var nflag = jsonObj.flag;
			if(nflag == 1)
			{
				alert("请选择正确的身份证图片！");
				return;
			}
		}
		else
		{
			var msg = "请选择正确的身份证图片！";
			alert(msg);
			return;
		}
	}
	else{
		var msg = "检测模块初始化未成功！";
		alert(msg);
		return;
	}
	
	ImgBackData = jsonObj.IdCardImg;
	document.getElementById("fileText2").value = ImgBackPath;
	var imgObj = document.getElementById("imghead2");
	showImg(imgObj, ImgBackPath, ImgBackData);
}


function click_IDCardOCRFront()
{
	
	if(ImgFrontData.length ==0)
	{
		alert("请先选择身份证照片！");
		return;
	}
	
	bIsClickOcrFront = true;
	showLoading();
	$("#question1").css("display","none");
	document.getElementById("result").innerHTML = "身份证识别中....";
	
	var nRet = cloudwalkobj.cwIdOcr(CW_StrSeverIp,CW_appID,CW_appSecret, 1, ImgFrontData);
	if(nRet == 1)
	{
		bIsClickOcrFront = false;
		document.getElementById("result").innerHTML = "";
		$("#question1").css("display","block");
		hideLoading();
		alert("请选择身份证图片");
		return;
	}

	
}

function click_IDCardOCRBack()
{
	if(ImgBackData.length ==0)
	{
		alert("请先选择身份证照片！");
		return;
	}
	
	bIsClickOcrBack = true;
	showLoading();
	$("#question2").css("display","none");
	document.getElementById("result2").innerHTML = "身份证识别中....";
	
	var nRet = cloudwalkobj.cwIdOcr(CW_StrSeverIp,CW_appID,CW_appSecret, 0, ImgBackData);
	if(nRet == 1)
	{
		bIsClickOcrBack = false;
		document.getElementById("result2").innerHTML = "";
		$("#question2").css("display","block");
		hideLoading();
		alert("请选择身份证图片");
		return;
	}

}

function openCramera()
{
	var nRet = cloudwalkobj.cwStartCamera(0,0);
	
	if(nRet != 0)
	{
		var msg = "打开摄像头失败错误码:"  + nRet;
		alert(msg);
	}
	
	return nRet
}

function closeCramera()
{
	cloudwalkobj.cwStopCamera();
}

function takePicture()
{
	var Tempimg = cloudwalkobj.cwTakePictures();
	if(Tempimg == null || Tempimg.length == 0)
	{
		alert("拍照失败！");
		return;
	}
	
	var jsonStr = cloudwalkobj.cwIDCardCheck(Tempimg);
	var jsonObj = $.parseJSON(jsonStr);
	if(jsonObj != null)
	{
		if(jsonObj.result == 0)
		{
			var nflag = jsonObj.flag;
			if(nflag == 0 && ImgObjName == "imghead")
			{
				alert("请先选择身份证正面照片！");
				return;
			}
			else if(nflag == 1 && ImgObjName == "imghead2")
			{
				alert("请先选择身份证反面照片！");
				return;
			}
		}
		else
		{
			var msg = "身份证检测失败错误码：" + jsonObj.result;
			alert(msg);
			return;
		}
	}
	else
	{
		var msg = "检测模块初始化未成功！";
		alert(msg);
		return;
	}
	
	if(ImgObjName == "imghead")
	{
		ImgFrontData = jsonObj.IdCardImg;
		document.getElementById("fileText").value = "";
		document.getElementById("result").innerHTML = "";
		$("#question1").css("display","block");
	}
	else if(ImgObjName == "imghead2")
	{
		ImgBackData =  jsonObj.IdCardImg;
		document.getElementById("fileText2").value = "";
		document.getElementById("result2").innerHTML = "";
		$("#question2").css("display","block");
	}
	
	var faceimg = document.getElementById(ImgObjName);
	if(isIE6_8())
	{
		var saveDir = "C:/cloudwalk/"; 					//图片保存的所在目录
		var filename = new Date().getTime()+".jpg";		//图片名称，如果文件名已存在则直接覆盖
		//var filename = "bestFace.jpg";				//文件名固定时，使用img.src = path;不能及时更新，还是显示上一张
		var filepath = saveDir + filename;
		var nret = cloudwalkobj.cwBase64ToFile(Tempimg, saveDir, filename);		//Base64保存为本地图片
		faceimg.src = 'file:///'+filepath;			//在Firefox下面必须添加file:///前缀才可以正确显示图片，IE有无皆可
	}
	else
	{
		faceimg.src = "data:image/jpeg;base64,"+ Tempimg;
	}
	
}

function PraseConfig(strJson)
{
	var jsonObj = $.parseJSON(strJson);
	if(jsonObj != "null")
	{
	 CW_StrSeverIp = jsonObj["ip"];
	 CW_appID =  jsonObj["appID"];
	 CW_appSecret =  jsonObj["appSecret"];
	 strLience = jsonObj["License"];
	}
}

function registerCallBackEach()
{
	//兼容谷歌
	//谷歌浏览器移除插件会析构，添加会创建
	var bDet=BrowserDetect.browser;
	if("Chrome"==bDet)
	{
		registerCallBack(cloudwalkobj, "cwIdCardOCRCallBack", idcardOcrEvent);
		var nret = cloudwalkobj.cwIDCardInit(strLience);
		if(nret != 0)
		{
			var msg = "初始化SDK失败错误码：" + nret;
			alert(msg);
		}
	}
}


